package com.example.safeassistan.train.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseVPFragment;
import com.example.safeassistan.train.adapter.TestOptionAdapter;
import com.example.safeassistan.train.model.OptionModel;
import com.example.safeassistan.train.model.SimulateAnswerProblemModel;
import com.example.safeassistan.utile.ToastUtil;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class ProblemSimulateMutliFragment extends BaseVPFragment implements View.OnClickListener{

    private View view;
    private Context context;
    private TextView tvType,tvTitle,tvResult,tvOkOption,tvUserOption;
    private RecyclerView rcView;
    private List<OptionModel>  options;
    private LinearLayout llResult;
    private TextView SureAnswer;
    private TestOptionAdapter optionAdapter;
    private int posi=0;//题目标号
    private int positions=0;//在每个分组里面的位置
    LocalBroadcastManager mLocalBroadcastManager;
    private SimulateAnswerProblemModel.ResultDTO.ManyListDTO data;
    private boolean isSelected=false;
    private String result="3";//1 正确  2 错误 3 未答题


    public static ProblemSimulateMutliFragment newInstance(int posi, int positions, SimulateAnswerProblemModel.ResultDTO.ManyListDTO data) {
        Bundle arguments = new Bundle();
        arguments.putInt("posi",posi);
        arguments.putSerializable("data",(Serializable) data);
        ProblemSimulateMutliFragment fl = new ProblemSimulateMutliFragment();
        fl.setArguments(arguments);
        return fl;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_similate_answer_problem, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) { }

    @Override
    protected void onFragmentFirstVisible(){
    }

    public void initView(){
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
        posi=getArguments().getInt("posi",1);
        positions=getArguments().getInt("positions",0);
        data=(SimulateAnswerProblemModel.ResultDTO.ManyListDTO)getArguments().getSerializable("data");
        rcView=view.findViewById(R.id.rcView);
        tvTitle=view.findViewById(R.id.tvTitle);
        tvType=view.findViewById(R.id.tvType);
        SureAnswer=view.findViewById(R.id.SureAnswer);
        SureAnswer.setOnClickListener(this::onClick);
        llResult=view.findViewById(R.id.llResult);
        tvResult=view.findViewById(R.id.tvResult);
        tvOkOption= view.findViewById(R.id.tvOkOption);
        tvUserOption=view.findViewById(R.id.tvUserOption);

        tvType.setText("多选题");
        SureAnswer.setVisibility(View.VISIBLE);

        tvTitle.setText(posi+"."+data.getQuestionsName());
        options=new ArrayList<>();
        String [] option=data.getAnswerContent().split(";");

        for(int i=0;i<option.length;i++){
            String [] s=option[i].split(":");
            options.add(new OptionModel(false,s[1]));
        }
         optionAdapter=new TestOptionAdapter(options);
        rcView.setLayoutManager(new LinearLayoutManager(getContext()));
        rcView.setAdapter(optionAdapter);
        optionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //选项的点击事件
                        if(options.get(position).isSelected()){
                            options.get(position).setSelected(false);
                        }else {
                            options.get(position).setSelected(true);
                        }
                        optionAdapter.notifyDataSetChanged();
            }
        });
   }
    int count=0;

    private String option="";
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.SureAnswer:
                isSelected=true;
                option="";
                for(int i=0;i<options.size();i++){
                    if(options.get(i).isSelected()){
                        count++;
                        switch (i){
                            case 0:
                               option=option+"A,";
                                break;
                            case 1:
                                option=option+"B,";
                                break;
                            case 2:
                                option=option+"C,";
                                break;
                            case 3:
                                option=option+"D,";
                                break;
                        }
                    }
                }
                if(count>1){
                    if (data.getAnswer().replace(",","").equals(option.replace(",",""))){
                        //正确了
                        tvResult.setText("回答正确");
                        result="1";
                        tvResult.setBackgroundResource(R.drawable.rightcorner_change_blue_bg);
                        tvUserOption.setTextColor(context.getResources().getColor(R.color.blue_3B5BEA));
                    }
//                    else if(data.getAnswer().contains(option)){
//                        //少选了
//                        result="1";
//                        tvResult.setText("回答半对");
//                        tvResult.setBackgroundResource(R.drawable.rightcorner_change_blue_bg);
//                        tvUserOption.setTextColor(context.getResources().getColor(R.color.blue_3B5BEA));
//                    }
                    else {
                        //选错了
                        result="2";
                        tvResult.setText("回答错误");
                        tvResult.setBackgroundResource(R.drawable.rightcorner_change_orange_bg);
                        tvUserOption.setTextColor(context.getResources().getColor(R.color.orange));
                    }
                    tvOkOption.setText(data.getAnswer());
                    tvUserOption.setText(option);
                    llResult.setVisibility(View.VISIBLE);

                    Intent intent = new Intent("com.example.safeassistan.ActivitySimulateAnswerProblem");
                    intent.putExtra("type",2);
                    intent.putExtra("posi",positions);
                    intent.putExtra("result",result);
                    intent.putExtra("option",option);
                    mLocalBroadcastManager.sendBroadcast(intent);
                }else {
                    Toast.makeText(context,"该题目是多选题，您必须选择两个及以上选项",Toast.LENGTH_SHORT).show();
                    isSelected=false;
                    if (isSelected){
                        //已经有选中的  说明已经判断出对错了 不能再改变答案了
                        ToastUtil toastUtil =new ToastUtil(context,R.layout.popu_success,"您已经选择完毕，不可重新选择",1);
                        toastUtil.show();
                        return;
                    }
                }
                break;
            default:
                break;
        }
    }
}
